<template>
  <div class="user-container">
    <div class="table-container">
      <avue-crud
        :data="data"
        :option="option"
        :page="page"
        v-model:page="page"
        @size-change="sizeChange"
        @current-change="currentChange"
        @search-change="searchChange"
        @search-reset="searchReset"
        @on-load="onLoad"
        :loading="loading"
      >
        <!-- 自定义操作按钮 -->
        <template #menu="{ row }">
          <el-button 
            type="primary" 
            size="small"
            @click="resetPassword(row)"
          >
            重置密码
          </el-button>
          <el-switch
            v-model="row.status"
            :active-value="1"
            :inactive-value="0"
            @change="updateStatus(row)"
          />
        </template>

        <!-- 自定义状态列 -->
        <template #status="{ row }">
          <el-tag :type="row.status === 1 ? 'success' : 'danger'">
            {{ row.status === 1 ? '正常' : '禁用' }}
          </el-tag>
        </template>
      </avue-crud>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getUserPage } from '@/api/user'

// 表格配置
const option = {
  align: 'center',
  menuAlign: 'center',
  searchMenuSpan: 6,
  border: true,
  index: true,
  viewBtn: true,
  editBtn: false,
  delBtn: false,
  page: true,
  pageSizes: [10, 20, 30, 50],
  paginationAlign: 'center',
  searchShowBtn: true,
  searchResetBtn: true,
  column: [
    {
      label: '用户ID',
      prop: 'id',
      width: 80
    },
    {
      label: '昵称',
      prop: 'nickname',
      search: true,
      searchPlaceholder: '请输入昵称',
      searchSpan: 6
    },
    {
      label: '手机号',
      prop: 'phone',
      search: true,
      searchPlaceholder: '请输入手机号',
      searchSpan: 6
    },
    {
      label: '邮箱',
      prop: 'email'
    },
    {
      label: '性别',
      prop: 'gender',
      type: 'select',
      dicData: [
        { label: '男', value: 1 },
        { label: '女', value: 2 },
        { label: '未知', value: 0 }
      ],
      width: 80
    },
    {
      label: '生日',
      prop: 'birthday',
      width: 100
    },
    {
      label: 'OpenID',
      prop: 'openid',
      width: 250,
      overHidden: true
    },
    {
      label: '会员',
      prop: 'isVip',
      type: 'select',
      dicData: [
        { label: '是', value: 1 },
        { label: '否', value: 0 }
      ],
      width: 80
    },
    {
      label: '状态',
      prop: 'status',
      type: 'select',
      dicData: [
        { label: '正常', value: 1 },
        { label: '禁用', value: 0 }
      ],
      width: 80
    },
    {
      label: '创建时间',
      prop: 'createTime',
      type: 'datetime',
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      width: 160
    },
    {
      label: '更新时间',
      prop: 'updateTime',
      type: 'datetime',
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      width: 160
    }
  ]
}

// 数据对象
const data = ref([])
const loading = ref(false)
const page = ref({
  total: 0,
  currentPage: 1,
  pageSize: 10
})

// 搜索改变
const searchChange = (params, done) => {
  page.value.currentPage = 1
  onLoad(page.value, params)
  done()
}

// 搜索重置
const searchReset = (done) => {
  page.value.currentPage = 1
  onLoad(page.value)
  done()
}

// 页码改变
const currentChange = (currentPage) => {
  page.value.currentPage = currentPage
  onLoad(page.value)
}

// 每页条数改变
const sizeChange = (pageSize) => {
  page.value.pageSize = pageSize
  onLoad(page.value)
}

// 加载数据
const onLoad = async (pagination, params = {}) => {
  try {
    loading.value = true
    const queryParams = {
      current: pagination?.currentPage || page.value.currentPage,
      size: pagination?.pageSize || page.value.pageSize,
      nickname: params.nickname || '',
      phone: params.phone || ''
    }
    
    const res = await getUserPage(queryParams)
    if (res.data.code === 200) {
      const { records, total, current, size } = res.data.data
      data.value = records
      page.value = {
        total,
        currentPage: current,
        pageSize: size
      }
    } else {
      ElMessage.error(res.data.msg || '获取用户列表失败')
    }
  } catch (error) {
    console.error('获取用户列表出错：', error)
    ElMessage.error('获取用户列表失败')
  } finally {
    loading.value = false
  }
}

// 更新用户状态
const updateStatus = async (row) => {
  try {
    // TODO: 调用更新用户状态的接口
    ElMessage.success('状态更新成功')
  } catch (error) {
    console.error('更新用户状态出错：', error)
    ElMessage.error('更新状态失败')
    row.status = row.status === 1 ? 0 : 1 // 恢复原状态
  }
}

// 重置密码
const resetPassword = async (row) => {
  try {
    await ElMessageBox.confirm('确定要重置该用户的密码吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    // TODO: 调用重置密码的接口
    ElMessage.success('密码重置成功，新密码已发送至用户手机')
  } catch (error) {
    if (error !== 'cancel') {
      console.error('重置密码出错：', error)
      ElMessage.error('重置密码失败')
    }
  }
}
</script>

<style scoped>
.user-container {
  background-color: var(--white-color);
  padding: var(--main-padding);
  border-radius: var(--border-radius);
  height: calc(100vh - 120px);
  display: flex;
  flex-direction: column;
  position: relative;
}

.table-container {
  flex: 1;
  overflow: auto;
  position: relative;
}

:deep(.el-tag) {
  border-radius: 12px;
  padding: 0 12px;
}

:deep(.el-button) {
  padding: 6px 16px;
  border-radius: 4px;
  margin-right: 8px;
}

:deep(.el-switch) {
  margin-left: 8px;
}

:deep(.avue-crud__pagination) {
  position: sticky;
  bottom: 0;
  background-color: var(--white-color);
  margin-top: 16px;
  padding: 16px 0;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
  z-index: 10;
}

:deep(.avue-crud__menu) {
  background-color: var(--white-color);
  margin-bottom: 16px;
  padding: 16px;
  border-radius: 4px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}
</style> 